@file:UseSerializers(ItemStackSerializer::class)

package com.lalameow.researchstation.api.technology

import com.lalameow.researchstation.ResearchStation
import com.lalameow.researchstation.translatable
import dev.latvian.mods.kubejs.stages.Stages
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.ItemStack
import net.minecraft.text.Text
import net.minecraft.util.math.BlockPos
import net.minecraft.world.World
import net.silkmc.silk.nbt.serialization.serializer.ItemStackSerializer

@Serializable
data class CommandReward(
    val commands: List<String>,
    override val name: Text = ResearchStation.translatable("rewards.command", commands)
) : TechnologyReward {
    override fun get(world: World, pos: BlockPos, player: PlayerEntity) {
        val commandManager = player.server!!.commandManager
        val commandSource = player.commandSource
        for (command in commands) commandManager.execute(commandSource, command)
    }
}

@Serializable
data class ItemReward(
    val items: List<ItemStack> = listOf(),
    override val name: Text = ResearchStation.translatable("rewards.item", items)
) : TechnologyReward {
    override fun get(world: World, pos: BlockPos, player: PlayerEntity) {
        val inventory = player.inventory
        for (item in items) {
            inventory.offerOrDrop(item)
        }
    }
}

@Serializable
data class StageReward(
    val stages: Set<String>,
    override val name: Text = ResearchStation.translatable("rewards.stage", stages)
) : TechnologyReward {
    override fun get(world: World, pos: BlockPos, player: PlayerEntity) {
        val playerStages = Stages.get(player)
        for (stage in stages) playerStages.add(stage)
    }
}
